package com.xiaoma.bingo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dexcoder.commons.bean.BeanConverter;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import com.xiaoma.bingo.entity.Resource;
import com.xiaoma.bingo.entity.RoleResource;
import com.xiaoma.bingo.vo.ResourceVo;

@Service
public class ResourceService {

    @Autowired
    private JdbcDao jdbcDao;
    
    public List<ResourceVo> findAllVos() {
        Criteria criteria = Criteria.select(Resource.class).asc("orderNum","id");
        List<Resource> resources = jdbcDao.queryList(criteria);
        List<ResourceVo> resVos = new ArrayList<ResourceVo>(resources.size());
        for (Resource r: resources) {
            ResourceVo vo = new ResourceVo();
            vo = BeanConverter.convert(vo, r);
            vo.set_parentId(r.getParentId());
            resVos.add(vo);
        }
        return resVos;
    }
    public List<Resource> findAll() {
        return jdbcDao.queryList(Resource.class);
    }
    
//    public List<Resource> findByIds(Integer[] ids) {
//        Criteria criteria = Criteria.select(Resource.class).where("id", "in", ids);
//        return jdbcDao.queryList(criteria);
//    }
    
    public void save(Resource resource) {
        jdbcDao.save(resource);
    }
    
    public void update(Resource resource) {
        jdbcDao.update(resource);
    }
    
    public void delete(int id) {
        Criteria criteria = Criteria.delete(Resource.class).where("id", new Object[]{id}).or("parentId", new Object[]{id});
        jdbcDao.delete(criteria);
    }
    
    public List<RoleResource> findRoleResource(RoleResource entity) {
        return jdbcDao.queryList(entity);
    }
    
    public int findChildrenCount(int id) {
        Resource entity = new Resource();
        entity.setParentId(id);
        return jdbcDao.queryCount(entity);
    }
    
}
